package com.axxc.jpa.task.rabbitmsg.receiver;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.axxc.jpa.entity.StudentEntity;
import com.axxc.jpa.out.model.Student;
import com.axxc.jpa.repository.IStudentRepository;

/**
 * @ClassName: RabbitMqReceiver
 * @Description: 消息消费者A 与 消息消费者B 分别均匀消费 队列(string_msg_queue) 中的消息
 *               多生产者一样,生产到相同的队列,分别均匀消费
 * @author xxc
 * @date 2018年8月3日 下午3:33:38
 * @see http://47.98.248.215:15672/#/
 */
@Component
@RabbitListener(queues = "user")
public class RabbitMqEntityMsgReceiverA {

	private Logger logger = LoggerFactory.getLogger(getClass());

	private List<StudentEntity> lists = new ArrayList<>();

	@Autowired
	private IStudentRepository studentRepository;

	@RabbitHandler
	public void processA(Student e) {

		if (e != null) {
			lists.add(e.toEntity());
		}
		logger.info("Receiver-消费者A lists size : {}", lists.size());
		if (lists.size() >= 50) {
			this.studentRepository.save(lists);
			// 保存完,清空,否则有重复的垃圾数据
			lists.clear();
			logger.info("current lists size : {}", lists.size());
		}
	}
}
